package com.weixing.mall.provider.controller.rpc;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.enums.ActTypeEnum;
import com.weixing.mall.provider.api.model.vo.ActGoodsVo;
import com.weixing.mall.provider.api.model.vo.ActRpcVo;
import com.weixing.mall.provider.model.domain.Activity;
import com.weixing.mall.provider.model.domain.ActivityGoods;
import com.weixing.mall.provider.model.enums.act.UpOrDownEnum;
import com.weixing.mall.provider.service.IActivityGoodsService;
import com.weixing.mall.provider.service.IActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wx
 * @Date 2020/1/9 15:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/activity/Goods")
public class ActivityGoodsRpcController extends BaseController {

    @Autowired
    private IActivityGoodsService activityGoodsService;
    @Autowired
    private IActivityService activityService;


    @GetMapping("selectByActIdAndItemId")
    public Result<ActGoodsVo> selectByActIdAndItemId(Long itemId, Long actId) {
        Activity activity = activityService.getById(actId);
        if (null == activity) {
            return ResultUtil.error("活动不存在");
        }
        ActivityGoods activityGoods = activityGoodsService.getOne(new QueryWrapper<ActivityGoods>().eq("act_id", actId)
                .eq("product_id", itemId));
        if (null == activityGoods) {
            return ResultUtil.error("商品未参加活动");
        }
        ActGoodsVo activityGoodsVo = new ActGoodsVo();
        ActRpcVo actRpcVo = new ActRpcVo();
        BeanUtils.copyProperties(activity, actRpcVo);
        actRpcVo.setActType(EnumUtil.getIEnum(ActTypeEnum.class,activity.getActType()));
        BeanUtils.copyProperties(activityGoods, activityGoodsVo);
        activityGoodsVo.setActRpcVo(actRpcVo);
        return ResultUtil.success(activityGoodsVo);
    }


    @GetMapping("selectByActIdAndGoodsId")
    public Result< List<ActGoodsVo>> selectByActIdAndGoodsId(Long goodsId, Long actId) {
        List<ActivityGoods> activityGoodsList = activityGoodsService.list(new QueryWrapper<ActivityGoods>().eq("act_id", actId)
                .eq("goods_id", goodsId).eq("up_down", UpOrDownEnum.UP.getValue()));
        List<ActGoodsVo> voList = Lists.newArrayList();
        ActGoodsVo activityGoodsVo = null;
        if (CollUtil.isNotEmpty(activityGoodsList)){
            for (ActivityGoods ag: activityGoodsList){
                activityGoodsVo = new ActGoodsVo();
                BeanUtils.copyProperties(ag, activityGoodsVo);
                voList.add(activityGoodsVo);
            }
        }
        return ResultUtil.success(voList);
    }


    @GetMapping("selectByActTypeAndItemId")
    public Result< List<ActGoodsVo>> selectByActTypeAndItemId(Integer actType, Long itemId) {
        List<ActivityGoods> activityGoodsList = activityGoodsService.list(new QueryWrapper<ActivityGoods>().eq("act_type", actType)
                .eq("item_Id", itemId));
        List<ActGoodsVo> voList = Lists.newArrayList();
        ActGoodsVo activityGoodsVo = null;
        if (CollUtil.isNotEmpty(activityGoodsList)){
            for (ActivityGoods ag: activityGoodsList){
                activityGoodsVo = new ActGoodsVo();
                BeanUtils.copyProperties(ag, activityGoodsVo);
                voList.add(activityGoodsVo);
            }
        }
        return ResultUtil.success(voList);
    }


//    @PostMapping("divideStock")
//    public Result<ActGoodsVo> divideStock(Long id, Integer cnt) {
//        activityGoodsService.divideStock(id,cnt);
//        return ResultUtil.success();
//    }

}
